import {createRouter,createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import Token from '../api/token.js'

// auth :代表是否登录才能访问, staff :代表是是否是企业账户 1代表企业账户
const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [{
			path: '/',
			name: 'home',
			component: HomeView,
			meta: {
				auth: true,
			}
		},
		{
			path: '/login',
			name: 'login',
			component: LoginView,
			meta: {
				auth: false,
			}
		},
		{
			path: '/find-pwd',
			name: 'find-pwd',
			component: () => import('../views/FindPasswordView.vue'),
			props: (route) => ({ is_staff: route.query.is_staff }) , //传给身份参数
			meta: {
				auth: false,
			}
		},
		{
			path: '/modify-pwd',
			name: 'modify-pwd',
			component: () => import('../views/ModifyPasswordView.vue'),
			meta: {
				auth: true,
			}
		},
		{
			path: '/task/add',
			name: "add-task",
			component: () => import('../views/task/TaskAddView.vue'), 
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/task/extra',
			name: "task-extra",
			component: () => import('../views/task/TaskExtraView.vue'),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/task/center',
			name: "task-center",
			component: () => import('../views/task/TaskCenterView.vue'),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/task/preview/:id',
			name: "task-preview",
			component: () => import('../views/task/TaskPreviewView.vue'),
			meta: {
				auth: true ,
			}
		},
		{
			path: '/task/code',
			name: "task-code",
			component: () => import('../views/task/TaskCodeView.vue'),
			meta: {
				auth: true ,
				staff: 0
			}
		},
		{
			path: '/task/update',
			name: "task-update",
			component: () => import('../views/task/TaskUpdateView.vue'),
			meta: {
				auth: true ,
				staff: 1
			}
		},


		{
			path: '/stage',
			name: "stage",
			component: () => import("../views/stage/StageView.vue"), 
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/stage/add',
			name: "add-stage",
			component: () => import("../views/stage/StageAddView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/stage/update',
			name: "stage-update",
			component: () => import("../views/stage/StageUpdateView.vue"),
			meta: {
				auth: true ,
				staff: 1
			}
		},
		{
			path: '/clazz',
			name: "clazz",
			component: () => import("../views/clazz/ClassView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/clazz/add',
			name: "clazz-add",
			component: () => import("../views/clazz/ClassAddView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/clazz/switch',
			name: "clazz-switch",
			component: () => import("../views/clazz/ClassSwitchView.vue"),
			meta: {
				auth: true ,
			}
		},
		{
			path: '/clazz/update',
			name: "clazz-update",
			component: () => import("../views/clazz/ClassUpdateView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/module',
			name: "module",
			component: () => import("../views/module/ModuleView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/module/add',
			name: "module-add",
			component: () => import("../views/module/ModuleAddView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/group',
			name: "group",
			component: () => import("../views/group/GroupView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/group/add',
			name: "group-add",
			component: () => import("../views/group/GroupAddView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/group/apply',
			name: "group-apply",
			component: () => import("../views/group/GroupApplyView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/group/update',
			name: "group-update",
			component: () => import("../views/group/GroupUpdateView.vue"),
			meta: {
				auth: true ,
				staff: 1
			}
		},
		{
			path: '/group/task-list',
			name: "group-task-list",
			component: () => import("../views/group/GroupTaskListView.vue"),
			meta: {
				auth: true ,
				staff: 1 
			}
		},
		{
			path: '/403',
			name: "forbidden",
			component: () => import("../views/Forbidden.vue"),
			meta: {
				auth: false ,
			}
		},
		{
			path: '/module/update',
			name: "module-update",
			component: () => import("../views/module/ModuleUpdateView.vue"),
			meta: {
				auth: true ,
				staff: 1
			}
		},
	]
})





// 配置全局路由守卫
router.beforeEach((to, from, next)=> {
	// 如果 访问的路由需要认证 
	if (to.meta.auth == null || to.meta.auth) {
		// 获取当前登录的用户
		const user = Token.getUser() ; 
		
		if (user == null) {
			next({name: "login"}) ; // 跳转到登录
		}else if (to.meta.staff == null || to.meta.staff == user.is_staff) {
			next() ; // 正常跳转
		}else{
			next({name: "forbidden"}) ;  // 禁止访问
		}
	}else{
		next()
	}
	
})


export default router